package com.sharedlease.controller;

import com.sharedlease.check.IllegalInfoFactory;
import com.sharedlease.entity.Illegal;
import com.sharedlease.entity.R;
import com.sharedlease.service.IllegalService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴钟富
 * @since 2021-04-06
 */
@RestController
@RequestMapping("/illegal")
public class IllegalController {

    @Autowired
    public IllegalService illegalService;

    @GetMapping("/getById/{id}")
    @ApiOperation("根据一个id查询非法词记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的非法词记录id", defaultValue = "1"),

    }
    )
    public Illegal getById(@PathVariable Long id) {
        Illegal illegal = illegalService.getById(id);
        return illegal;
    }

    @GetMapping("/list")
    @ApiOperation("查询全部非法词记录")
    public List<Illegal> list() {
        List<Illegal> list = illegalService.list(null);
        return list;
    }

    @PostMapping("/save")
    @ApiOperation("新增一条非法词记录")
    public R save(@RequestBody Illegal illegal) {
        IllegalInfoFactory.addData(illegal.getWord());
        return R.operate(illegalService.save(illegal));
    }

    @PutMapping("/update")
    @ApiOperation("根据id更改一条非法词记录")
    R update(@RequestBody Illegal illegal) {
        if (illegalService.updateById(illegal)) {
            Illegal illegal1 = getById(illegal.getId());
            IllegalInfoFactory.deleteData(illegal1.getWord());
            IllegalInfoFactory.addData(illegal.getWord());
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除一条非法词记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的非法词记录id", defaultValue = "-1"),

    }
    )
    R removeById(@PathVariable Long id) {
        IllegalInfoFactory.deleteData(getById(id).getWord());
        return R.operate(illegalService.removeById(id));
    }

}

